import {_decorator, Label} from 'cc';
import {BaseComponent} from "db://assets/scripts/framework/BaseComponent";
import Message from "db://assets/scripts/framework/Message";
import {UIManager} from "db://assets/scripts/framework/UIManager";
import {MessageType} from "db://assets/scripts/framework/MessageType";

const {ccclass, property} = _decorator;

@ccclass('HPController')
export class HPController extends BaseComponent {

    hp: number = 100;

    start() {
        // 注册
        UIManager.instance.registerReceiver(this);
    }

    update(deltaTime: number) {

    }

    receiveMessage(message: Message) {
        switch (message.command) {
            case MessageType.UI_REFRESH_HP:
                this.changeHP(<number>message.content);
                break;
        }
    }

    /**
     * 改变血量
     * 加血：+10
     * 减血：-10
     *
     * @param hp 血量
     */
    changeHP(hp: number) {
        // 更改血量
        this.hp = this.hp + hp;
        // 显示血量
        this.node.getComponent(Label).string = this.hp.toString();
    }
}

